<template>
  <div class="real-video-content">
    <div class="real-box" v-show="showToolTree">
      <treeSelect
        @selectNode="setPointData"
        @selectBackTime="selectBackTime"
        :ptzCode="currentPtzCode"
        @selectOneNode="selectOneNode"
      ></treeSelect>
    </div>
    <div class="real-video-box" id="pabox" :class="{'isback': isback}">
      <div class="top-box">
        <div class="l">
          <div class="btn" @click="showTree">
            <img
              class="btnicon"
              :style="{
                transform: showToolTree ? 'rotate(180deg)' : 'rotate(0deg)',
              }"
              src="@/assets/video/arrow.png"
              alt=""
            />
            <span class="text">{{
              showToolTree ? "收起目录" : "展开目录"
            }}</span>
          </div>
          <div class="btn" v-if="currentPoint.tag" @click="setFullScreen">
            <span class="text">全屏</span>
          </div>
          <div class="btn" @click="selectOneNode({})">
            <img class="btnicon" src="@/assets/video/gamer.png" alt="" />
            <span class="text">全部关闭</span>
          </div>
        </div>
        <div class="r">
          <div
            class="one"
            @click="setLayout(1)"
            :class="{ active: currentActive == 1 }"
          >
            <div class="icon"></div>
          </div>
          <div
            class="more l4"
            @click="setLayout(4)"
            :class="{ active: currentActive == 4 }"
          >
            <div class="icon" v-for="item in 4" :key="item"></div>
          </div>
          <div
            class="more l6"
            @click="setLayout(6)"
            :class="{ active: currentActive == 6 }"
          >
            <div class="icon" v-for="item in 6" :key="item"></div>
          </div>
          <!-- <div
          class="more l9"
          @click="setLayout(9)"
          :class="{ active: currentActive == 9 }"
        >
          <div class="icon" v-for="item in 9" :key="item"></div>
        </div> -->
        </div>
      </div>
      <div class="video-boxx" v-if="currentPoint.tag">
      <!-- <div class="video-boxx"> -->
        <!-- <videoRtsp
          ref="videoRef"
          :showToolbar="0"
          :layout="layout1"
          :vid="allPoint"
          :width="1221"
          :height="680"
          @choseIndex="setIndexcode"
          :isback="isback"
          :startTime="backTime.length > 0 ? backTime[0] : ''"
          :endTime="backTime.length > 0 ? backTime[1] : ''"
        >
        </videoRtsp> -->
        <videoRtsp
          ref="videoRef"
          :showToolbar="0"
          :layout="layout1"
          :onePid="currentPoint"
          :currentActive="currentActive"
          :width="isback ? 1180 : 1221"
          :height="680"
          @choseIndex="setIndexcode"
          @choseGamerIndexCode="setIndexcode"
          :isback="isback"
          :startTime="backTime.length > 0 ? backTime[0] : ''"
          :endTime="backTime.length > 0 ? backTime[1] : ''"
        >
        </videoRtsp>
      </div>

      <div class="empty" v-else>
        <img src="@/assets/images/novideo.png" />
        <p>暂无视频</p>
      </div>
    </div>
  </div>
</template>

<script setup>
import SelfDialog from "@/components/dialog/dialog.vue";
import videoRtsp from "./component/videoRtsp.vue";
import treeSelect from "./component/treeSelect.vue";
import { nextTick, onMounted } from "vue";
const route = useRoute(); // 获取当前路由信息
let layout1 = ref("2x2");
let allPoint = ref([]);
let videoRef = ref();

let showToolTree = ref(true); // 是否显示工具树

let catchPoint = ref([]);
// 设置布局
let currentActive = ref(4);
// 设置点位
let currentPtzCode = ref("");
const setPointData = (point) => {
  try {
    catchPoint.value = point || [];
    if (currentActive.value == 1) {
      allPoint.value = point.slice(0, 1);
    }
    if (currentActive.value == 4) {
      allPoint.value = point.slice(0, 4);
    }
    if (currentActive.value == 6) {
      allPoint.value = point.slice(0, 6);
    }
    if (currentActive.value == 9) {
      allPoint.value = point.slice(0, 9);
    }
    // currentPtzCode.value = allPoint.value.at(-1);
    if (allPoint.value.length > 0) {
      currentPtzCode.value = allPoint.value[allPoint.value.length - 1];
    }
  } catch (e) {
    console.error(e);
  }
};

const setIndexcode = (code) => {
  currentPtzCode.value = code;
};

const setLayout = async (type) => {
  currentActive.value = type;
  if (type == 1) {
    layout1.value = "1x1";
  }
  if (type == 4) {
    layout1.value = "2x2";
  }
  if (type == 6) {
    layout1.value = "1+5";
  }
  if (type == 9) {
    layout1.value = "3x3";
  }
  setPointData(catchPoint.value);
};
let isback = ref(false);
let backTime = ref([]);
const searchTime = () => {
  videoRef.value && videoRef.value.playBack();
};
const selectBackTime = (time) => {
  console.log(time);
  backTime.value = time;
};

// 展开收起树
const showTree = () => {
  showToolTree.value = !showToolTree.value;
  nextTick(() => {
    videoRef.value && videoRef.value.resize();
  });
};

// 全屏
const setFullScreen = () => {
  videoRef.value && videoRef.value.setFullScreen();
};

let currentPoint = ref({});
// 单选指定窗口播放视频
const selectOneNode = (point) => {
  currentPoint.value = point;
};

onMounted(() => {
  if (route.params.type == "back") {
    isback.value = true;
  }
});
</script>
<style lang="scss" scoped>
.real-video-content {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: top;
  position: relative;
  display: flex;
  align-items: top;
  .real-box {
    width: 375px;
    height: 100%;
    background: #ffffff;
    box-shadow: 0px 0px 29px 0px rgba(70, 102, 149, 0.06);
    border-radius: 12px;
    overflow: hidden;
    margin-right: 20px;
  }
  .real-video-box {
    flex: 1;
    height: 100%;
    background: #ffffff;
    box-shadow: 0px 0px 29px 0px rgba(70, 102, 149, 0.06);
    border-radius: 12px;
    padding: 33px 28px;
    position: relative;
    &.isback{
      padding: 33px 28px 33px 88px;
    }
    .video-boxx {
      width: 100%;
      height: calc(100% - 56px);
    }
  }
  .empty {
    width: 120px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    font-family: PingFangSC, PingFang SC;
    font-weight: 500;
    font-size: 14px;
    color: #2263bf;
    text-align: center;
  }
  .top-box {
    // position: absolute;
    // right: 64px;
    // top: 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 20px;
    .r {
      display: flex;
      align-items: center;
    }
    .l {
      display: flex;
      align-items: center;
      .btn {
        cursor: pointer;
        display: flex;
        align-items: center;
        padding: 0 12px;
        height: 32px;
        align-items: center;
        background: rgba(142, 191, 255, 0.32);
        border-radius: 3px;
        border: 1px solid rgba(44, 132, 255, 0.3);
        margin-right: 16px;
        .text {
          font-family: PingFangSC, PingFang SC;
          font-weight: 500;
          font-size: 14px;
          color: #2263bf;
        }
        .btnicon {
          margin-right: 6px;
        }
      }
    }
    .one,
    .more {
      width: 36px;
      height: 36px;
      cursor: pointer;
      margin-left: 16px;
      &.active {
        background: rgba(142, 191, 255, 0.32);
        border-radius: 4px;
        border: 1px solid rgba(44, 132, 255, 0.3);
        .icon {
          background: #2263bf !important;
        }
      }
    }
    .one {
      padding: 7px;
      .icon {
        width: 22px;
        height: 22px;
        background: #9ac4ff;
      }
    }
    .more {
      display: flex;
      align-items: center;
      justify-content: space-around;
      flex-wrap: wrap;
      padding: 7px;
      gap: 4px;
      &.l4 {
        .icon {
          width: 40%;
          height: 40%;
          background: #9ac4ff;
        }
      }
      &.l6 {
        padding: 4px;
        gap: 2px;
        .icon {
          width: 20%;
          height: 40%;
          background: #9ac4ff;
        }
      }
      &.l9 {
        padding: 4px;
        gap: 2px;
        .icon {
          width: 20%;
          height: 20%;
          background: #9ac4ff;
        }
      }
    }
  }
  .selectTime {
    position: absolute;
    left: 165px;
    top: 31px;
    display: flex;
    align-items: center;
    .select-time-label {
      display: inline-block;
      width: 62px;
      height: 20px;
      font-family: PingFangSC, PingFang SC;
      font-weight: 500;
      font-size: 14px;
      color: #333333;
    }
  }
}
</style>